// pages/apps/poi/point/point-list/point-list.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showLoading: true,
    baseImgUrl: getApp().globalData.IMG_URL,
    pointsList: [],
    latitude: 26.53422,
    longitude: 106.7521,
    net_msg: '',
    error_msg: '正在刷新...',
    isFirstEnter: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    const that = this
    // 加载缓存
    const pointsList = wx.getStorageSync('pointsList') || []
    if (pointsList != null && pointsList.length > 0) {
      this.setData({ pointsList: pointsList });
      this.setData({ showLoading: true })
      // console.error("onLoad: " + JSON.stringify(pointsList))
    } else {
      console.error("onLoad: 无数据")
    }

    that.setData({ showLoading: true })

    that.setData({
      latitude: getApp().globalData.latitude,
      longitude: getApp().globalData.longitude
    })
    that.getNearbyTradeAreaPage()

    // wx.getNetworkType({
    //   success(res) {
    //     const networkType = res.networkType
    //     if (networkType == 'none') {
    //       that.setData({ showLoading: false })
    //       that.setData({ net_msg: '当前网络不可用' })

    //       wx.showModal({
    //         title: '网络异常',
    //         content: '当前网络异常，请检查后重试！',
    //         showCancel: false,
    //         confirmText: '重新加载',
    //         success(res) {
    //           if (res.confirm) {
    //             console.log('用户点击确定')
    //             that.setData({ showLoading: true })
    //             that.getLocation()

    //           } else if (res.cancel) {
    //             console.log('用户点击取消')
    //           }
    //         }
    //       })
    //     } else {
    //       that.setData({ showLoading: true })
    //       that.setData({ net_msg: '' })
    //       that.getLocation()
    //     }
    //   }
    // })
    // this.initNetworkStatusChange()

    // this.setData({ isFirstEnter: false })
  },
  // initNetworkStatusChange() {
  //   const that = this
  //   const listener = function (res) {
  //     if (res.isConnected) {
  //       that.setData({ net_msg: '' })
  //       that.setData({ showLoading: true })
  //       that.getLocation()
  //     } else {
  //       that.setData({ net_msg: '当前网络不可用' })
  //       that.setData({ showLoading: false })
  //     }
  //   }

  //   wx.offNetworkStatusChange(listener)
  //   wx.onNetworkStatusChange(listener)
  // },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // if (!this.data.isFirstEnter) {
    //   this.getLocation()
    //   console.error("onShow()-getLocation:")
    // }
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    // setTimeout(()=>{
    //   wx.stopPullDownRefresh()
    // },2000)
  },
  /**
   * 打开详情
   */
  toPointDetail(param: any) {
    var item = param.currentTarget.dataset.bean
    wx.reLaunch({
      url: '/pages/apps/poi/point/point-detail/point-detail?' +
        'pointid=' + item.id + '&name=' + item.name + '&type=1'
    })
  },
  getNearbyTradeAreaPage() {
    var that = this
    const preUrl = getApp().globalData.SERVER_URL
    const url = preUrl + "fc/tradeArea/open/listForNearby"
    // console.error("url: " + url)
    wx.request({
      url: url,
      method: 'GET',
      data: {
        pageNo: 1,
        pageSize: 100,
        // type: '1',
        latForLoc: this.data.latitude,
        lngForLoc: this.data.longitude
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded', // 默认值
        // 'X-Access-Token': token
      },
      success(res: any) {
        // console.error("结果: " + JSON.stringify(res))
        wx.stopPullDownRefresh()
        if (res.data.code == 200) {
          const list = res.data.result.records;
          if (list != null && list.length > 0) {
            for (let index = 0; index < list.length; index++) {
              const item = list[index];
              if (item.logo != null) {
                var logoArr = item.logo.split(',') as string[]
                item.logo = logoArr[0]
              }
            }
          }
          that.setData({ pointsList: list })
          wx.setStorageSync('pointsList', list)
        } else {
          console.info(JSON.stringify(res))
        }
      },
      complete(res) {
        that.setData({ showLoading: false })
        that.setData({ net_msg: '' })
      }
    })
  },

  getLocation() {
    var that = this
    console.error("getLocation-start: 开始")
    wx.getLocation({
      type: 'gcj02',
      isHighAccuracy: true,
      success(res) {
        // console.error("getLocation-success:" + JSON.stringify(res))
        const lat = res.latitude
        const lng = res.longitude
        that.setData({ latitude: lat, longitude: lng })

        that.getNearbyTradeAreaPage()

        getApp().globalData.latitude = lat
        getApp().globalData.longitude = lng
      },
      fail(res) {
        console.error("getLocation-fail:" + JSON.stringify(res))
      }
    })
  },
  /**
   * 打开导航
   */
  toParkingNavi(param: any) {
    const item = param.currentTarget.dataset.bean
    console.error("toParkingNavi:" + JSON.stringify(item))
    wx.openLocation({
      latitude: item.lat, // 纬度，范围为-90~90，负数表示南纬
      longitude: item.lng, // 经度，范围为-180~180，负数表示西经
      scale: 13, // 缩放比例
      name: item.name,
      address: item.address,
      success: function (r) {
        console.log(r)
      }
    })
  }
})